﻿/* Author: Tomer Paz
   games.com
   2007
*/
// the purpose of this package is to immitate server behavior and protocol
// for Testing GameController and CMD using Local connection to another Flash client (i.e. pseudo server)

package monopoly.ServerStub {
	
	import flash.net.LocalConnection;
	import monopoly.CMD.CMD;		// this is only for 'abusing' the CMD constants definitions...
	
	// we expect server demo to implement exactly the same functions the client CMD does (only with Server implementation...)
	public class ServerLocalConnection
		{
		private var _ServerChannel:String = "_MyChannel";
		private var _ServerChannelReceipt:String = "_MyChannelReceipt";
		private var _receiver:LocalConnection;
		
		public function ServerLocalConnection() 
			{
			// Create the local connection object in the receiving movie. (this is the receiving movie)
			_receiver = new LocalConnection( );
			}
		
		public function get ServerChannel():String { return _ServerChannel; }
		public function set ServerChannel(Channel:String):void 
			{
			_ServerChannel = Channel;
			_ServerChannelReceipt = _ServerChannel + "Receipt";
			}
		
		// do All work here
		public function Execute():void 
			{
			// Instruct the local connection instance to listen for messages
			// sent over the selected channel.
			_receiver.connect( _ServerChannel );
			
			// handle incoming messages in "ICMD" like functions - need to use the redirect-client option of LocalConnection class
			// for that to work best for this test
			_receiver.client = this;
			}
				
		// cmd code = 120 - returns Allow/Deny
		function introduce(ClientID:String, ClientVersion:String):Boolean
			{
			trace("Recieved ClienID: " + ClientID + " Client Version:" + ClientVersion);
			// sending the "HELO" line info..., approve client acess with a receipt to its own channel
			_receiver.send(_ServerChannelReceipt, "receipt", INTRO, true);
			return(true);
			}
		// cmd code = 160 - returns ready/not ready	
		function ready(PlayerType:int):Boolean
			{
			trace("player ready - player type: " + PlayerType);

			_receiver.send(_ServerChannelReceipt, "Reciept", PLAYER_TYPE, true);
			
			return true;
			}
			
		// cmd code = 200 - fills referenced parmeters.
		// this is one of the few cases where the Server and client have reverese rolls- 
		// Server is the Broadcaster and the client listens
		// and wait for server command.
		// Returns (send) Mode - wait X seconds or Move (0 for move, >0 how many seconds to wait)
		// the mode shall be part of the sent PlayersStatus Array
		// for the demo we shall update only about Move operation
		function set_status(Status:Array):void
			{
			// just for the sake of testing:
			var Wait:int = Math.round(Math.random()*3);
			
			Status[0] = Wait;
			
			if (Wait > 0) 
				{
				trace("wait " + Wait + " seconds");
				}
			else
				{
				trace("Move Your Ass !");
				}
			
			
			// this is the line to remain after removing tests. NOTE! in this exceptional case
			// we do not RPC the client "Receipt" function but rather the 'listenning' set_status function!!
			_receiver.send(_ServerChannelReceipt, "set_status", STATUS, Status);
			}
		
		/* now the game commands */
		
		// returns the dies result - 400
		function throw_dies():uint
			{
			// at last, do something Real in this demo
			var Rand:uint = Math.round(Math.random()*12 + 1);
			
			_receiver.send(_ServerChannelReceipt, "Reciept", THROW_DIES, Rand);
			
			return Rand;
			}
		// client returns desired move and expect server approval (allow/deny) - 420
		function done_move(NewPosition:uint):Boolean
			{
			var answer:Boolean = false;
			// server verifies new position is acceptable bla bla and returns "OK/BAD" = true/false
			// we shall just immitate that in the demo with very basic test
			if (NewPosition > 0)	// and in real game server it will probably test the position against Previous one, and dies result etc'
				answer = true;
				
			return answer;
			}
		// client disconnect session - 999
		function CMDdisconnect():void
			{
			_receiver.close( );
			}
			
		/* property actions */
	
		// action none - 430
		function ac_none():Boolean
			{
			}
		// action pay  - 440 - pay to owner
		function ac_pay(Player:Object, Sum:Number):Boolean
			{
			}
		// action buy  - 450
		function ac_buy(Player:Object, PropertyID:uint, Sum:Number):Boolean
			{
			}
		// action sell - 460
		function ac_sell(Player:Object, PropertyID:uint, Sum:Number):Boolean
			{
			}
		function ac_mortgage(Player:Object, PropertyID:uint, Sum:Number):Boolean
			{
			}
		// check and verify Player is not obligated to any more actions ! - retuns success/fail- 555
		function end_turn():Boolean
			{
			_receiver.send(_ServerChannelReceipt, "Reciept", ACTION_ENDTURN, true);
			return true;
			}
		}
}